package cn.jn.lhm.netty.part05;

import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.embedded.EmbeddedChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/7/17 - 22:46
 **/
@Slf4j
public class TestEmbeddedChannel {
    public static void main(String[] args) {
        ChannelHandler h1 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("1");
                super.channelRead(ctx, msg);
            }
        };

        ChannelHandler h2 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("2");
                super.channelRead(ctx, msg);
            }
        };

        ChannelHandler h3 = new ChannelOutboundHandlerAdapter() {
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("3");
                super.write(ctx, msg, promise);
            }
        };

        ChannelHandler h4 = new ChannelOutboundHandlerAdapter() {
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("4");
                super.write(ctx, msg, promise);
            }
        };

        EmbeddedChannel channel = new EmbeddedChannel(h1, h2, h3, h4);
        channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("inbound".getBytes(StandardCharsets.UTF_8)));
        channel.writeOutbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("outbound".getBytes(StandardCharsets.UTF_8)));
    }
}
